package manager;

import java.util.ArrayList;

import painter4ViewHelper.Painter;
import painter4ViewHelper.PainterProvider;
import painter4ViewHelper.TextPainter;

public class TimeManager implements PainterProvider {

	private TextPainter timePainter;
	private long timeBefore;
	private long time;
	private boolean beginFlag;
	
	public TimeManager() {
		timePainter = new TextPainter(100, 100);
		beginFlag = false;
		init();
	}
	
	public void init() {
		time = 0;
		timeBefore = System.currentTimeMillis();
	}

	public void start() {
		beginFlag = true;
		init();
	}
	
	public void stop() {
		beginFlag = false;
	}
	
	@Override
	public ArrayList<Painter> getPainters() {
		ArrayList<Painter> result = new ArrayList<>();
		timePainter.setText(String.valueOf(getTime()));
		result.add(timePainter);
		return result;
	}
	
	public void pause() {
		update();
	}
	
	public void resume() {
		timeBefore = System.currentTimeMillis();
	}
	
	public long getTime() {
		if(!beginFlag) return 0;
		update();
		return time;
	}

	public void update() {
		long timeCurrent = System.currentTimeMillis();
		time += timeCurrent - timeBefore;
		timeBefore = timeCurrent;
	}

}
